<template>
  <div class="bc-drawing">
    <Info :detail="detail" />
    <c-section title="设计区域及面积明细">
      <!-- 十月页面优化START -->
      <BCTable ref="table" :table-data="tableData" :detail="detail" @on-tabledata-change="onTableDataChange" />
      <!-- 十月页面优化END -->
    </c-section>
    <div class="btn-container">
      <c-button type="primary" @click="saveHandle">保存</c-button>
      <c-button v-if="hasFinished" type="primary" @click="nextStepHandle">下一步</c-button>
      <c-button v-else type="primary" @click="submitHandle">提交</c-button>
    </div>
  </div>
</template>
<script>
import Info from './components/Info'
import BCTable from './components/BCTable'
import { getBasicsInfo, postQueryAreaSeries, postDetailTaskAreaPage, addOutpicAreaMaintain, postDetailCommit } from '@/api/shopDrawing'
export default {
  name: 'ShopDrawingDetailManagementBCDrawing',
  components: {
    Info,
    BCTable
  },
  data() {
    return {
      filtersRaw: [
        { key: 'enable', value: 1, operator: '=' }
      ],
      detail: {},
      tableData: [],
      filterDesignSpace: [],
      hasFinished: false
    }
  },
  computed: {
    // hasFinished() {
    //   return this.tableData.some(e => e.areaCode === 'Q014' || e.areaCode === 'Q015')
    // }
  },
  created() {
    this.getDetail()
  },
  methods: {
    onTableDataChange(v) {
      this.hasFinished = v
    },
    getDetail() {
      getBasicsInfo(this.$route.query.id).then(res => {
        this.detail = res.data
        this.filterDesignSpace = this.$split(this.detail.designSpace)
        this.getAreaSeries()
      })
    },
    queryData() {
      const fr = []
      const p = {}
      p.isReportPic = '1'
      return [fr, p]
    },
    getAreaSeries() {
      const otc = this.$route.query.outpicDetailTaskCode
      const designRange = this.$route.query.designRange
      let type
      if (designRange === 'DOOR') {
        type = 'door'
      }
      postDetailTaskAreaPage(otc, type).then(res => {
        if (res.data.records.length === 0) {
          return postQueryAreaSeries(this.$route.query.id, ...this.queryData()).then(res => {
            return res
          })
        }
        return res
      }).then(res => {
        this.tableData = res.data.records.filter(e => { // this.detail.isRedsign
          return this.$route.query.state === 'B' || (
            ([1, '1'].includes(e.isReportPic) && this.filterDesignSpace.includes(e.areaCode)) || [0, '0'].includes(e.isReportPic)
          )
        }).map(e => {
          for (const [k, v] of Object.entries(e)) {
            if (v === 'null') {
              e[k] = ''
            }
          }
          if (e.picTaskCode === undefined && e.picTaskName === this.detail.picTaskName) {
            e.picTaskCode = this.detail.picTaskCode
          }
          if (this.detail.detailTaskSchedule === 'A') {
            e.isDetailOutpic = '1'
          }
          return e
        })
      })
    },
    // 保存数据
    saveData() {
      return this.$refs.table.validate().then(tableData => {
        const data = {
          id: this.$route.query.id,
          outpicDetailTaskCode: this.$route.query.outpicDetailTaskCode,
          picBatchCode: this.$route.query.picBatchCode,
          designAreaSeriesDTOList: tableData
        }
        if (this.hasFinished) {
          return addOutpicAreaMaintain(data)
        } else {
          return postDetailCommit(data, true)
        }
      })
    },
    // 保存
    saveHandle() {
      return this.saveData().then(res => {
        this.$message.success('保存成功！')
        this.$store.dispatch('views/goFirst')
      })
    },
    nextStepHandle() {
      return this.saveData().then(res => {
        this.$message.success('操作成功！')
        this.$router.push({ name: 'ShopDrawingDetailManagementCategory', query: { ...this.$route.query }})
      })
    },
    submitHandle() {
      return this.saveData().then(res => {
        this.$message.success('操作成功！')
        this.$store.dispatch('views/goBack')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.bc-drawing{

}
</style>
